home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / RIBPointsPolygons.m < prev    next >
Encoding:
Text File  |  1995-03-25  |  5.4 KB  |  233 lines

  1. // copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
  2. // see COPYRIGHT for reuse legalities
  3. //
  4.  
  5. #import "RIBPointsPolygons.h"
  6.  
  7. @implementation RIBPointsPolygons
  8.  
  9. + initialize { return [RIBPointsPolygons setVersion:1], self; }
  10.  
  11. - (BOOL)hasBoundingBox { return YES; }
  12.  
  13. - init
  14. {
  15.   [super init];
  16.  
  17.   nPolys = 0;
  18.   nVertices = NULL;
  19.   vertices = NULL;
  20.   nPolysBuf = (char *)malloc(128);
  21.   cntBuf = (char *)malloc(128);
  22.   
  23.   return self;
  24. }
  25.  
  26. - free
  27. {
  28.   if (nVertices) { free(nVertices); }
  29.   if (vertices) { free(vertices); }
  30.   free(nPolysBuf);
  31.   free(cntBuf);
  32.  
  33.   return [super free];
  34. }
  35.  
  36.  
  37. // this is used only internally, for setting a copy without freeing the original
  38. - _setNVertices:(RtInt *)newNVertices vertices:(RtInt *)newVertices
  39. {
  40.   int  i, cnt = 0;
  41.  
  42.  
  43.   if (newNVertices)
  44.   {  nVertices =  (long *)NXZoneMalloc([self zone], nPolys*sizeof(long));
  45.      for (i = 0; i < nPolys; i++)  
  46.      {  nVertices[i] = newNVertices[i];
  47.         cnt += nVertices[i];
  48.      }
  49.      vertices =  (long *)NXZoneMalloc([self zone], cnt*sizeof(long));
  50.      for (i = 0; i < cnt; i++)  
  51.      {  vertices[i] = newVertices[i];
  52.      }
  53.      
  54.   }
  55.   else
  56.   {  nVertices = NULL;
  57.      vertices = NULL;
  58.   }
  59.   return self;
  60. }
  61.  
  62. - _reallocBufs  {  nPolysBuf = (char *)malloc(128);  cntBuf = (char *)malloc(128); return self; }
  63.  
  64. - copyFromZone:(NXZone *)zone
  65. {
  66.    id   newCopy = [super copyFromZone:zone];
  67.  
  68.   [newCopy _setNVertices:nVertices vertices:vertices];
  69.   [newCopy _reallocBufs];
  70.   return newCopy;
  71. }
  72.  
  73.  
  74. - setNPolys:(RtInt)newNPolys nVertices:(RtInt *)newNVertices vertices:(RtInt *)newVertices 
  75.      n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
  76.      printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
  77. {  
  78.    nPolys = newNPolys;
  79.    nVertices = newNVertices;
  80.    vertices = newVertices;
  81.    [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];
  82.  
  83.    dirtyBoundingBox = TRUE;
  84.    return self;
  85. }
  86.  
  87. - setNPolys:(RtInt)newNPolys { nPolys = newNPolys; dirtyBoundingBox = TRUE; return self; }
  88. - setNVertices:(RtInt *)newNVertices { nVertices = newNVertices; dirtyBoundingBox = TRUE; return self; }
  89. - setVertices:(RtInt *)newVertices { vertices = newVertices; dirtyBoundingBox = TRUE; return self; }
  90.  
  91. - (RtInt)nPolys { return nPolys; }
  92. - (RtInt *)nVertices { return nVertices; }
  93. - (RtInt *)vertices { return vertices; }
  94.  
  95. - (BOOL)theSameAs:otherRIBCommand
  96. {
  97.   int    i,
  98.          nVerticesSum = 0;
  99.   RtInt  *otherNVertices, *otherVertices;
  100.  
  101.  
  102.   if ([self class] != [otherRIBCommand class])
  103.   {  return NO;
  104.   }
  105.   if (nPolys != [otherRIBCommand nPolys])
  106.   {  return NO;
  107.   }
  108.  
  109.   otherNVertices = [otherRIBCommand nVertices];
  110.   for (i = 0; i < nPolys; i++)
  111.   {  if (nVertices[i] != otherNVertices[i])
  112.      {  return NO;
  113.      }
  114.      nVerticesSum += nVertices[i];
  115.   }
  116.  
  117.   otherVertices = [otherRIBCommand vertices];
  118.   for (i = 0; i < nVerticesSum; i++)
  119.   {  if (vertices[i] != otherVertices[i])
  120.      {  return NO;
  121.      }
  122.   }
  123.  
  124.   return [super theSameAs:otherRIBCommand];
  125. }
  126.  
  127.  
  128. - (BOOL)similarTo:otherRIBCommand
  129. {
  130.   int    i,
  131.          nVerticesSum = 0;
  132.   RtInt  *otherNVertices, *otherVertices;
  133.  
  134.  
  135.   if ([self class] != [otherRIBCommand class])
  136.   {  return NO;
  137.   }
  138.   if (nPolys != [otherRIBCommand nPolys])
  139.   {  return NO;
  140.   }
  141.  
  142.   otherNVertices = [otherRIBCommand nVertices];
  143.   for (i = 0; i < nPolys; i++)
  144.   {  if (nVertices[i] != otherNVertices[i])
  145.      {  return NO;
  146.      }
  147.      nVerticesSum += nVertices[i];
  148.   }
  149.  
  150.   otherVertices = [otherRIBCommand vertices];
  151.   for (i = 0; i < nVerticesSum; i++)
  152.   {  if (vertices[i] != otherVertices[i])
  153.      {  return NO;
  154.      }
  155.   }
  156.  
  157.   return [super theSameAs:otherRIBCommand];
  158. }
  159.  
  160.  
  161. - renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  162. {
  163.   RiPointsPolygonsV(nPolys, nVertices, vertices, n, tokens, parms);
  164.   return self;
  165. }
  166.  
  167. - (BOOL)isMotionBlurrable { return YES; }
  168.  
  169. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  170. {
  171.    int  i, sum = 0;
  172.  
  173.  
  174.    for (i = 0; i < tab; i++)
  175.    {  NXPrintf(stream, "\t");
  176.    }
  177.    NXPrintf(stream, "PointsPolygons {");
  178.    for (i = 0; i < nPolys; i++)
  179.    {  NXPrintf(stream, "%d ", nVertices[i]);
  180.      sum += nVertices[i];
  181.    }
  182.    NXPrintf(stream, "} {");
  183.    for (i = 0; i < sum; i++)
  184.    {  NXPrintf(stream, "%d ", vertices[i]);
  185.    }
  186.    NXPrintf(stream, "} ");
  187.  
  188.    [super writeParameterList:stream];
  189.    return self;
  190. }
  191.  
  192. #define typeVector "l"
  193. #define typeValues &nPolys
  194.  
  195. - read:(NXTypedStream*)stream 
  196. {
  197.     int version, i, cnt = 0;
  198.     
  199.     [super read:stream];
  200.     nPolysBuf = (char *)malloc(128);
  201.     cntBuf = (char *)malloc(128);
  202.     version = NXTypedStreamClassVersion(stream,"RIBGeneralPolygon");
  203.     if (version == 0) NXReadTypes(stream,"i",&version), version=1;
  204.     if (version == 1) {
  205.     NXReadTypes(stream,typeVector,typeValues);
  206.     nVertices = (long *)NXZoneMalloc([self zone], nPolys*sizeof(long));
  207.     NXReadArray(stream, "l", nPolys, nVertices);
  208.     for (i = 0; i < nPolys; i++) {
  209.         cnt += nVertices[i];
  210.     }
  211.     vertices = (long *)NXZoneMalloc([self zone], cnt*sizeof(long));
  212.     NXReadArray(stream, "l", cnt, vertices);
  213.     } else {
  214.     }
  215.     return self;
  216. }
  217.  
  218. - write:(NXTypedStream*)stream 
  219. {
  220.     int   i, cnt = 0;
  221.     
  222.     [super write:stream];
  223.     NXWriteTypes(stream, typeVector, typeValues);
  224.     NXWriteArray(stream, "l", nPolys, nVertices);
  225.     for (i = 0; i < nPolys; i++) {
  226.     cnt += nVertices[i];
  227.     }
  228.     NXWriteArray(stream, "l", cnt, vertices);
  229.     return self;
  230. }
  231.  
  232. @end
  233.